#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int dx[4] = { 0, 0, 1, -1 };
    int dy[4] = { 1, -1, 0, 0 };
    int getMaximumGold(vector<vector<int>>& grid) {
        vector<vector<bool>> map(grid.size(), vector<bool>(grid[0].size(), false));
        int ret = 0;
        for (int i = 0; i < grid.size(); i++)
            for (int j = 0; j < grid[i].size(); j++)
            {
                if (grid[i][j] == 0) 0;
                else ret = max(ret, dfs(grid, map, i, j));
                cout << map[i][j] << ' ';
            }
        return ret;
    }
    int dfs(vector<vector<int>>& grid, vector<vector<bool>>& map, int x, int y)
    {
        int m = 0;
        for (int k = 0; k < 4; k++)
        {
            int a = x + dx[k], b = y + dy[k];
            if (a >= 0 && a < grid.size() && b >= 0 && b < grid[a].size() && grid[a][b] != 0 && map[a][b] == false)
            {
                map[x][y] = true;
                m = max(m, dfs(grid, map, a, b));
                map[x][y] = false;
            }
        }
        return m + grid[x][y];
    }
};